package com.hz.supermanage.dao;

import java.util.*;

import javax.servlet.http.HttpServletRequest;

import com.hz.framework.dao.CommonDao;
import com.hz.supermanage.bean.*;

public class OrganizationDao extends CommonDao{
	/**
     * 节点是否有子存在
     * @param nodeId
     * @return
     */
    public boolean isHasChild(String nodeId)  {
    	String hql = "select count(*) from Hzsysorganization where parentid='" + nodeId + "' and depstate='" + Constants.NODE_STATE_ACTIVE + "'";
        System.out.println("isHasChild:"+hql);
        return Integer.valueOf(this.selHqlObj(hql).toString()) != 0;
    }
    /**
     * 是否有根节点
     * @return
     * @throws ConnectionException
     * @throws EntityOperationException
     */
    public boolean isHasRoot()  {
        String hql = "select count(*) from Hzsysorganization where parentid is null and depstate='" + Constants.NODE_STATE_ACTIVE + "'";
        System.out.println("isHasRoot:"+hql);
        return Integer.valueOf(this.selHqlObj(hql).toString()) != 0;
    }
    /**
     * 返回节点的类型
     * @param type
     * @return
     */
    public String getNodeIcon(String type) {
        if (Constants.NODE_TYPE_ORG.equals(type)) {
            return "/parts/images/img/org.gif";
        } else if (Constants.NODE_TYPE_DEPT.equals(type)) {
            return "/parts/images/img/dept.gif";
        } else if (Constants.NODE_TYPE_POS.equals(type)) {
            return "/parts/images/img/pos.gif";
        }
        return "";
    }
    /**
     * 获得子的集合
     * @param uuid
     * @return
     */
    public Collection getChildNodes(String parentUuid)  {
    	String hql="from Hzsysorganization where parentid ='"+parentUuid+"' and depstate='" + Constants.NODE_STATE_ACTIVE + "' order by CONVERT(deporder ,SIGNED) asc";
        return this.selHqlList(hql);
    }
    /**
     * 获得指定的单位
     * @param primaryKey
     * @return
     * @throws Exception
     */
    public List getOrgNode(String primaryKey) throws Exception {
    	String hql="from Hzsysorganization where parentid ='"+primaryKey+"' and depstate='" + Constants.NODE_STATE_ACTIVE + "' order by CONVERT(deporder ,SIGNED) asc";
        return this.selHqlList(hql);
    }
    /**
     * 查询单位根节点
     * @author 
     * @time Sep 28, 2010	
     * @return 
     * 方法功能:
     */
    public List queryRootList(){
    	String hql="from Hzsysorganization where parentid ='-1' order by CONVERT(deporder ,SIGNED) asc";
    	System.out.println("queryRootList:"+hql);
    	return this.selHqlList(hql);
    }
    /**
     * 模糊查询 
     * @param nodeName
     * @return
     */
    public List queryLikeNodeName(String depname,String pt){
    	String hql="from Hzsysorganization where depname like '%" + depname + "%'";
    	if(!"".equals(pt)){
    		hql += " and url like '%"+pt+"%'";
    	}
    	hql += " order by CONVERT(deporder ,SIGNED) asc";
    	System.out.println("queryLikeNodeName:"+hql);
    	return this.selHqlList(hql);
    }
    /**
     * 根据唯一depid查询Hzsysorganization相关信息
     * @author 
     * @time Sep 28, 2010	
     * @param depid
     * @return 
     * 方法功能:
     */
    public Hzsysorganization queryNodeInfo(String depid){
    	String hql="from Hzsysorganization where depid = '"+ depid+"' order by CONVERT(deporder ,SIGNED) asc";
    	Hzsysorganization dep = (Hzsysorganization) this.selHqlObj(hql);
    	System.out.println("queryNodeInfo:"+hql);
    	return dep;
    }
    /**
     * 编写xml信息
     * @author 
     * @time Sep 28, 2010	
     * @param dep
     * @return 
     * 方法功能:
     */
    public String generalTMIXml(Hzsysorganization dep){
    	StringBuffer sbf= new StringBuffer();
    	String parentid=dep.getParentid()==null?"":dep.getParentid();
    	String depname=dep.getDepname()==null?"":dep.getDepname();
    	String depcontent=dep.getDepcontent()==null?"":dep.getDepcontent();
    	String deporder=dep.getDeporder()==null?"":dep.getDeporder().toString();
    	String depnumber=dep.getDepnumber()==null?"":dep.getDepnumber();
    	String depid=dep.getDepid()==null?"":dep.getDepid();
    	String deplink=dep.getDeplink()==null?"":dep.getDeplink();
    	sbf.append("<depid><![CDATA["+depid+"]]></depid>");
    	sbf.append("<parentid><![CDATA["+parentid+"]]></parentid>");
    	sbf.append("<depname><![CDATA["+depname+"]]></depname>");
    	sbf.append("<depcontent><![CDATA["+depcontent+"]]></depcontent>");
    	sbf.append("<deporder><![CDATA["+deporder+"]]></deporder>");
    	sbf.append("<depnumber><![CDATA["+depnumber+"]]></depnumber>");
    	sbf.append("<deplink><![CDATA["+deplink+"]]></deplink>");
    	return sbf.toString();
    }
    /**
     * 查询对应子节点
     * @author 
     * @time Sep 28, 2010	
     * @param orgId
     * @return 
     * 方法功能:
     */
    public List getOrgDeptsSelect(String orgId)  {
    	String hql = "from Hzsysorganization where parentid='" + orgId + "' and depstate='" + Constants.NODE_STATE_ACTIVE + "' order by CONVERT(deporder ,SIGNED) asc";
        System.out.println("getOrgDeptsSelect:"+hql);
        return this.selHqlList(hql);
    }
    /**
     * 生成岗位select
     * @author 
     * @time Sep 28, 2010	
     * @param orgId
     * @return 
     * 方法功能:
     */
    public List getDeptPosSelect(String orgId)  {
    	String hql = "from Hzsysorganization where parentid='" + orgId + "' and depstate='" + Constants.NODE_STATE_ACTIVE + "' order by CONVERT(deporder ,SIGNED) asc";
        System.out.println("getDeptPosSelect:"+hql);
        return this.selHqlList(hql);
    }
    
    /**
     * 根据唯一键查询人员信息表
     * @param uuid
     * @return Map
     */
    public Map getPersonSelect(String uuid)  {
    	String hql = "select new map(p.personid as personid, p.loginid as loginid, p.personname as personname, " +
    				"p.orgid as orgid,p.mobiletelephone as mobiletelephone, dd.depname as deptid, dp.depname as posid, p.sex as sex, " +
    				"p.birthday as birthday, p.workday as workday, p.folk as folk, p.hometown as hometown, " +
    				"p.idcard as idcard, p.health as health, p.mastdegree as mastdegree, p.certificate as certificate, " +
    				"p.email as email, p.telephone as telephone, p.address as address, p.marriage as marriage, " +
    				"p.genro as genro, p.remark as remark, p.perdrder as perdrder,p.password as password,p.loginid as loginid,p.gid as gid) from Hzsyspersonnel p " +
    				"left join p.posid dp left join p.deptid as dd where p.personid ='" + uuid + "'";
        System.out.println("getPersonSelect:"+hql);
        
        return (Map)this.selHqlObj(hql);
    }
    /**
     * 根据唯一键查询单位名称
     * @param orgid
     * @return Object
     */
    public Object getOrgNameSelect(String orgid)  {
    	String hql = "select depname as depname from Hzsysorganization where depid = '"+ orgid+"' order by CONVERT(deporder ,SIGNED) asc";
        System.out.println("getOrgNameSelect:"+hql);
        return this.selHqlObj(hql);
    }
    /**
     * 查询人员任职信息
     * @param orgid
     * @return
     */
    public Map getHoldValue(String empid)  {
    	Map emEmpPos = new HashMap();
    	String sql = "select id ,e.perid,e.posid,e.orgid,e.depid,e.ptype,dp.depname as posname ,dd.depname as depname, "+
    	"(case when ptype='1' then '隶属' when ptype='2' then '分管' else '兼职' end) as ptypevalue "+
    	"from Hzsysempholdpost e left join Hzsysorganization dp on e.posid=dp.depid left join Hzsysorganization dd on e.depid=dd.depid " +
    	"where  e.perid = '" + empid+"'";
    	
        System.out.println("getHoldValue:"+sql);
        try {
			List listdate = this.DBjsonList(sql);
			emEmpPos.put("Collection", listdate);
			
			List listcolum = new ArrayList();
			Map mz = new HashMap();
			mz.put("key", "depname");
			listcolum.add(mz);
			
			mz = new HashMap();
			mz.put("key", "posname");
			listcolum.add(mz);
			
			mz = new HashMap();
			mz.put("key", "ptypevalue");
			listcolum.add(mz);
			
			mz = new HashMap();
			mz.put("key", "ptype");
			listcolum.add(mz);
			
			mz = new HashMap();
			mz.put("key", "posid");
			listcolum.add(mz);
			
			emEmpPos.put("Columns", listcolum);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
         
        return emEmpPos;
    }
    /**
     * 
     * @author 韩飞
     * @time Sep 29, 2010	
     * @param empid
     * @param request
     * @return 
     * 方法功能:
     */
    public Map getEmpHoldInfo(String empid , HttpServletRequest request)  {
    	String sql = "select e.personid,e.personname,e.posid,e.orgid,e.deptid,dp.depname as posname ,dd.depname as depname,do.depname as orgname"+
    	" from Hzsyspersonnel e left join Hzsysorganization dp on e.posid=dp.depid left join Hzsysorganization dd on e.deptid=dd.depid left join Hzsysorganization do on e.orgid=do.depid" +
    	" where  e.personid = " + empid;
        System.out.println("getEmpHoldInfo:"+sql);
        List list = this.selHqlList("from Hzsysorganization where deptype = '"+Constants.NODE_TYPE_ORG+"'");
        request.setAttribute("value", list);
        
        return this.DBjsonMap(sql);
    }
    /**
     * 任职类别转换
     * @param state
     * @return String
     */
    public static String getPosTypeDesc(String state){
		if ("1".equals(state))
			return "隶属";
		if ("2".equals(state))
			return "分管";
		if ("3".equals(state))
			return "兼职";
		else
			return "错误类型";
	}
	/**
	 * 
	 * @author 牟国斌
	 * @time 2010-11-8	
	 * @return Relationship list
	 * 方法功能:通过Id获取全部关系人
	 */
 /*   public List<Relationship> getralationlist(Integer personid){
    	String hql="from Relationship where pid=?";
		SqlParameter sqlpara=new SqlParameter();
		sqlpara.add(personid);
		List<Relationship>rlist =this.list(hql, sqlpara);
    	return rlist;
    }*/
}
